#include <Windows.h>
#include <tchar.h>
#include <assert.h>

#define		WM_CAPTURESRCN	(WM_APP + 101)
#define		WM_CAPTURECTRL	(WM_APP + 102)

HWND _CreateDisplayWnd(HWND hParent);
void _DemoAnimateWnd(HWND hWnd);

LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	static const UINT IDC_BTNDEMO	= 1001;
	static const UINT IDC_BTNEXIT	= 1003;

	static HWND hWndDisplay;

	switch (nMsg)
	{
		case WM_CREATE:
		{
			//--: Move Main wnd to center of screen
			RECT rtMainWnd;
			GetClientRect(hWnd, &rtMainWnd);
			UINT nSrnWidth		= GetSystemMetrics(SM_CXFULLSCREEN);
			UINT nSrnHeight		= GetSystemMetrics(SM_CYFULLSCREEN);
			UINT nWndWidth		= (rtMainWnd.right - rtMainWnd.left);
			UINT nWndHeight		= (rtMainWnd.bottom - rtMainWnd.top);
			UINT nLeft			= (nSrnWidth - nWndWidth) / 2;
			UINT nTop			= (nSrnHeight - nWndHeight) / 2;
			MoveWindow(hWnd, nLeft, nTop, nWndWidth, nWndHeight, TRUE);

			CreateWindow(_T("Button"), _T("Demo AnimateWnd"), WS_VISIBLE|WS_CHILD, 0, 0, 150, 50, hWnd, (HMENU)IDC_BTNDEMO, NULL, NULL);
			CreateWindow(_T("Button"), _T("Exit"), WS_VISIBLE|WS_CHILD, 480, 0, 100, 50, hWnd, (HMENU)IDC_BTNEXIT, NULL, NULL);
		
			hWndDisplay = _CreateDisplayWnd(hWnd);
			break;
		}
		case WM_COMMAND:
		{
			
			switch (wParam)
			{
				case IDC_BTNDEMO:
				{
					if (NULL != hWndDisplay)
					{
						EnableWindow(GetDlgItem(hWnd, IDC_BTNDEMO), FALSE);
						_DemoAnimateWnd(hWndDisplay);
						EnableWindow(GetDlgItem(hWnd, IDC_BTNDEMO), TRUE);
					}
					break;
				}
			
				case IDC_BTNEXIT:
				{
					DestroyWindow(hWnd);
					break;
				}
			}
			break;
		}
		case WM_PAINT:
		{
			PAINTSTRUCT pt;
			HDC hDC = BeginPaint(hWnd, &pt);

			EndPaint(hWnd, &pt);
			return 0;
		}
		case WM_LBUTTONDOWN:
		{
			PostMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, lParam);
			break;
		}
		case WM_DESTROY:
		{
			
			PostQuitMessage(0);
			break;
		}
	}

	return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
bool RegisterWndClass(const TCHAR* szClsName, WNDPROC fnWndProc)
{
	WNDCLASSEX wcs		= {0};
	wcs.cbSize			= sizeof(WNDCLASSEX);
	wcs.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wcs.lpszClassName	= szClsName;
	wcs.lpfnWndProc		= fnWndProc;
	wcs.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcs.hIcon			= LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_APPLICATION));
	wcs.style			= CS_VREDRAW|CS_HREDRAW;
	
	return (0 != RegisterClassEx(&wcs));
}

//////////////////////////////////////////////////////////////////////////
HWND CreateWnd(const TCHAR* szClsName, const TCHAR* szWndCaption, HWND hParent = NULL, 
			   DWORD dwStyle = WS_POPUP|WS_VISIBLE|WS_SYSMENU|WS_CAPTION, 
			   DWORD dwStyleEx = 0)
{
	if (NULL != szClsName)
	{
		HWND hWnd = CreateWindowEx(dwStyleEx, szClsName, szWndCaption, dwStyle, 0, 0, 800, 480, hParent, NULL, GetModuleHandle(NULL), NULL);
	
		if (NULL != hWnd && IsWindow(hWnd))
		{
			return hWnd;
		}
	}
	return NULL;
}

//////////////////////////////////////////////////////////////////////////
void RunMessageLoop()
{
	MSG Msg;
	while (true)
	{
		while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (WM_QUIT == Msg.message)
			{
				return;
			}

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}

//////////////////////////////////////////////////////////////////////////
BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName		= _T("Win32App");
	const TCHAR* szWndCaption	= _T("Demo0049");

	if (RegisterWndClass(szClsName, WndProc))
	{
		if (NULL != CreateWnd(szClsName, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
HWND _CreateDisplayWnd(HWND hParent)
{	
	HWND hWnd;

	const TCHAR* szClsName = _T("DisplayWnd");
	if (RegisterWndClass(szClsName, DefWindowProc))
	{
		hWnd = CreateWnd(szClsName, NULL, hParent, WS_POPUPWINDOW, 0);
		SetWindowLongPtr(hWnd, GWL_EXSTYLE, GetWindowLongPtr(hWnd, GWL_EXSTYLE) | WS_EX_STATICEDGE);
	}

	return hWnd;
}
